﻿<UserControl x:Class="BillsManager.Views.BillsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:Caliburn="http://www.caliburnproject.org"
             xmlns:Commanding="clr-namespace:BillsManager.ViewModels.Commanding;assembly=BillsManager.ViewModels"
             xmlns:Controls="clr-namespace:BillsManager.Views.Controls"
             xmlns:Converters="clr-namespace:BillsManager.Views.Converters"
             xmlns:DataTemplates="clr-namespace:BillsManager.Views.DataTemplates"
             xmlns:DesignTime.ViewModels="clr-namespace:BillsManager.DesignTime.ViewModels;assembly=BillsManager.DesignTime"
             xmlns:Loc="clr-namespace:BillsManager.Localization;assembly=BillsManager.Localization"
             xmlns:ViewModels="clr-namespace:BillsManager.ViewModels;assembly=BillsManager.ViewModels"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             Caliburn:Bind.AtDesignTime="True"
             d:DesignHeight="250"
             d:DesignWidth="900"
             mc:Ignorable="d">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!--<ResourceDictionary Source="Dictionaries\Styles.xaml" />-->
                <ResourceDictionary Source="Themes\Themes.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <UserControl.Background>
        <Binding Mode="OneTime" Source="{StaticResource View.Background}" />
    </UserControl.Background>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="{StaticResource View.Region.Spacing.Normal}" />
            <!--<RowDefinition Height="Auto" />-->
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <TextBlock FontSize="{StaticResource View.Region.Title.FontSize}"
                   FontWeight="{StaticResource View.Region.Title.FontWeight}"
                   Foreground="{StaticResource View.Region.Title.Foreground}"
                   Text="{Loc:Translate Bills}"
                   Visibility="{StaticResource View.Region.Title.Visibility}" />

        <!--<Rectangle Grid.Row="2"
                   Height="1"
                   HorizontalAlignment="Stretch"
                   VerticalAlignment="Center"
                   Fill="{StaticResource View.Foreground.Disabled}" />-->

        <Controls:ListViewEx x:Name="lsvBills"
                             Grid.Row="3"
                             AlternationCount="0"
                             Grid.IsSharedSizeScope="True"
                             ItemDoubleLeftClickCommand="{Binding ShowBillDetailsCommand}"
                             ItemDoubleLeftClickCommandParameter="{Binding SelectedBillViewModel}"
                             ItemsSource="{Binding FilteredBillViewModels,
                                                   IsAsync=True}"
                             SelectedItem="{Binding SelectedBillViewModel}"
                             SelectionMode="Single"
                             VirtualizingPanel.IsVirtualizing="True"
                             VirtualizingPanel.VirtualizationMode="Recycling">
            <!--
            <ListView.ItemTemplate>
            <DataTemplate>
            <DataTemplates:BillDataTemplate />
            </DataTemplate>
            </ListView.ItemTemplate>
            -->

            <Controls:ListViewEx.View>
                <GridView>
                    <GridViewColumn Width="155"
                                    DisplayMemberBinding="{Binding SupplierName}"
                                    Header="{Loc:Translate Supplier}">
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock FontWeight="{StaticResource GridView.ColumnHeader.FontWeight}" Text="{TemplateBinding Content}" />
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="95"
                                    DisplayMemberBinding="{Binding Code}"
                                    Header="{Loc:Translate Number}">
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock FontWeight="{StaticResource GridView.ColumnHeader.FontWeight}" Text="{TemplateBinding Content}" />
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="95" Header="{Loc:Translate Amount}">
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock HorizontalAlignment="Right"
                                           FontWeight="{StaticResource GridView.ColumnHeader.FontWeight}"
                                           Text="{TemplateBinding Content}" />
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Amount, StringFormat=\{0:N2\} €}" TextAlignment="Right" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="70" Header="{Loc:Translate Paid_toBill}">
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock HorizontalAlignment="Center"
                                           FontWeight="{StaticResource GridView.ColumnHeader.FontWeight}"
                                           Text="{TemplateBinding Content}" />
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                                    <Path x:Name="tick"
                                          Width="{StaticResource View.FontSize}"
                                          Height="{StaticResource View.FontSize}"
                                          Data="{StaticResource Checkmark3}"
                                          Fill="{StaticResource PaidBrush}"
                                          Stretch="Uniform"
                                          Visibility="{Binding IsPaid,
                                                               Converter={StaticResource BooleanToVisibilityConverter},
                                                               Mode=OneWay}" />
                                    <Path x:Name="cross"
                                          Width="{StaticResource View.FontSize}"
                                          Height="{StaticResource View.FontSize}"
                                          Data="{StaticResource CloseGeometry}"
                                          Fill="{StaticResource NotPaidBrush}"
                                          Stretch="Uniform"
                                          Visibility="{Binding IsNotPaid,
                                                               Converter={StaticResource BooleanToVisibilityConverter},
                                                               Mode=OneWay}" />
                                </StackPanel>

                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}, Mode=FindAncestor}}" Value="True">
                                        <Setter TargetName="tick" Property="Fill" Value="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" />
                                        <Setter TargetName="cross" Property="Fill" Value="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" />
                                    </DataTrigger>
                                </DataTemplate.Triggers>

                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="110" Header="{Loc:Translate Release}">
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock HorizontalAlignment="Center"
                                           FontWeight="{StaticResource GridView.ColumnHeader.FontWeight}"
                                           Text="{TemplateBinding Content}" />
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock HorizontalAlignment="Center"
                                           Text="{Binding ReleaseDate,
                                                          StringFormat=\{0:d\}}"
                                           TextAlignment="Right" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="110" Header="{Loc:Translate Due}">
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock HorizontalAlignment="Center"
                                           FontWeight="{StaticResource GridView.ColumnHeader.FontWeight}"
                                           Text="{TemplateBinding Content}" />
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>

                                <Border x:Name="border"
                                        Margin="0 -5"
                                        HorizontalAlignment="Stretch"
                                        VerticalAlignment="Stretch"
                                        CornerRadius="{StaticResource Control.Border.CornerRadius}"
                                        Padding="5">
                                    <TextBlock x:Name="dueDate"
                                               HorizontalAlignment="Center"
                                               Text="{Binding DueDate,
                                                              StringFormat=\{0:d\}}"
                                               TextAlignment="Right" />
                                </Border>
                                <DataTemplate.Triggers>

                                    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}, Mode=FindAncestor}}" Value="True">
                                        <Setter TargetName="border" Property="CornerRadius" Value="0" />
                                    </DataTrigger>

                                    <!--  {x:Static ViewModels:DueAlert.Low}  -->
                                    <DataTrigger Binding="{Binding DueAlert, Mode=OneWay}" Value="Low">
                                        <Setter TargetName="border" Property="Background" Value="{StaticResource LowDueAlertBrush}" />
                                        <Setter TargetName="dueDate" Property="Foreground" Value="{StaticResource Button.Foreground.Normal}" />
                                    </DataTrigger>


                                    <DataTrigger Binding="{Binding DueAlert, Mode=OneWay}" Value="Medium">
                                        <Setter TargetName="border" Property="Background" Value="{StaticResource MediumDueAlertBrush}" />
                                        <Setter TargetName="dueDate" Property="Foreground" Value="{StaticResource Button.Foreground.Normal}" />
                                    </DataTrigger>

                                    <DataTrigger Binding="{Binding DueAlert, Mode=OneWay}" Value="High">
                                        <Setter TargetName="border" Property="Background" Value="{StaticResource HighDueAlertBrush}" />
                                        <Setter TargetName="dueDate" Property="Foreground" Value="{StaticResource Button.Foreground.Normal}" />
                                    </DataTrigger>

                                    <DataTrigger Binding="{Binding DueAlert, Mode=OneWay}" Value="Critical">
                                        <Setter TargetName="border" Property="Background" Value="{StaticResource CriticalDueAlertBrush}" />
                                        <Setter TargetName="dueDate" Property="Foreground" Value="{StaticResource Button.Foreground.Normal}" />
                                    </DataTrigger>

                                </DataTemplate.Triggers>

                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="85" Header="{Loc:Translate Agio}">
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock HorizontalAlignment="Right"
                                           FontWeight="{StaticResource GridView.ColumnHeader.FontWeight}"
                                           Text="{TemplateBinding Content}" />
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Agio, StringFormat=\{0:N2\} €}" TextAlignment="Right" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="85" Header="{Loc:Translate Costs}">
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock HorizontalAlignment="Right"
                                           FontWeight="{StaticResource GridView.ColumnHeader.FontWeight}"
                                           Text="{TemplateBinding Content}" />
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding AdditionalCosts, StringFormat=\{0:N2\} €}" TextAlignment="Right" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="Auto" Header="{Loc:Translate Notes}">
                        <GridViewColumn.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock FontWeight="{StaticResource GridView.ColumnHeader.FontWeight}" Text="{TemplateBinding Content}" />
                            </DataTemplate>
                        </GridViewColumn.HeaderTemplate>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Notes}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </Controls:ListViewEx.View>

            <!--
            <ListView.ItemContainerStyle>
            <Style BasedOn="{StaticResource {x:Type ListViewItem}}"
            TargetType="{x:Type ListViewItem}">
            <Setter Property="ContextMenu">
            <Setter.Value>

            </Setter.Value>
            </Setter>
            </Style>
            </ListView.ItemContainerStyle>
            -->

            <Controls:ListViewEx.ContextMenu>
                <ContextMenu>

                    <MenuItem Command="{Binding PayBillCommand,
                                                Mode=OneWay}"
                              CommandParameter="{Binding SelectedBillViewModel}"
                              Header="{Loc:Translate Pay}">
                        <MenuItem.Icon>
                            <Controls:AutoGreyableImage Source="Images/generic/pay-16.png" />
                        </MenuItem.Icon>
                    </MenuItem>

                    <Separator />

                    <MenuItem Command="{Binding AddNewBillCommand, Mode=OneWay}" Header="{Loc:Translate Add}">
                        <MenuItem.Icon>
                            <Controls:AutoGreyableImage Source="Images/generic/add-16.png" />
                        </MenuItem.Icon>
                    </MenuItem>

                    <MenuItem Command="{Binding EditBillCommand,
                                                Mode=OneWay}"
                              CommandParameter="{Binding SelectedBillViewModel}"
                              Header="{Loc:Translate Edit}">
                        <MenuItem.Icon>
                            <Controls:AutoGreyableImage Source="Images/generic/edit-16.png" />
                        </MenuItem.Icon>
                    </MenuItem>

                    <MenuItem Command="{Binding DeleteBillCommand,
                                                Mode=OneWay}"
                              CommandParameter="{Binding SelectedBillViewModel}"
                              Header="{Loc:Translate Delete}">
                        <MenuItem.Icon>
                            <Controls:AutoGreyableImage Source="Images/generic/delete-16.png" />
                        </MenuItem.Icon>
                    </MenuItem>

                    <Separator />

                    <MenuItem Command="{Binding ShowBillDetailsCommand,
                                                Mode=OneWay}"
                              CommandParameter="{Binding SelectedBillViewModel}"
                              Header="{Loc:Translate Details}">
                        <MenuItem.Icon>
                            <Controls:AutoGreyableImage Source="Images/generic/details-16.png" />
                        </MenuItem.Icon>
                    </MenuItem>

                </ContextMenu>
            </Controls:ListViewEx.ContextMenu>

        </Controls:ListViewEx>

    </Grid>

</UserControl>